package org.smr.ministore.security.component.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smr.ministore.entities.Authority;
import org.smr.ministore.entities.Role;
import org.smr.ministore.entities.User;
import org.smr.ministore.micro.client.FeignUserMicroServiceClient;
import org.smr.ministore.security.component.user.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Component
public class ConcreteUserDetailService  implements UserDetailsService {

    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger(ConcreteUserDetailService.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private FeignUserMicroServiceClient userMicroService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.debug("loadUserByUsername username:{}",username);

        User user = userMicroService.queryByUserName(username);
        if( ObjectUtils.isEmpty(user)){
            logger.debug("用户名:{} 不存在！", username );
            throw new UsernameNotFoundException("Username not found!");
        }

        List<Role>  roleList = userMicroService.getRoleByUser(user.getId());
        List<Authority> authorities = userMicroService.getAuthoritiesByUser(user.getId());

        org.springframework.security.core.userdetails.User securityUser =
                new org.springframework.security.core.userdetails.User(
                        user.getUserName(),
                        user.getPassword(),
                        createAuthorities(roleList));
        //UserDetails userDetails = new UserDetailsImpl(  user, roleList,  authorities);

        return securityUser;
    }


    /**
     * 权限字符串转化
     *
     * 如 "USER,ADMIN" -> SimpleGrantedAuthority("USER") + SimpleGrantedAuthority("ADMIN")
     *
     * @param roleList 权限字符串
     */
    private List<SimpleGrantedAuthority> createAuthorities(List<Role> roleList){

        List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        for ( Role role : roleList ) {
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority(role.getCode()));
        }
        return simpleGrantedAuthorities;
    }

    public FeignUserMicroServiceClient getUserMicroService() {
        return userMicroService;
    }

    public void setUserMicroService(FeignUserMicroServiceClient userMicroService) {
        this.userMicroService = userMicroService;
    }
}